package vista;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import bean.JTextFieldValidator;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VistaProducto extends javax.swing.JFrame {
	private JButton btnagregar;
	private JButton btnquitar;
	private JTable tblIngrediente;
	private JTable tblAgregados;
	private JLabel jLabel1;
	private JSpinner spnProcion;
	private DefaultTableModel modeloIngrediente,modeloAgregados;
	private JButton btnregistrar;
	private JLabel lblcategoria;
	private JComboBox cmbcategoria;
	private JLabel lblcodigo;
	private JTextField txtcodigo;
	private JLabel lbldescripcion;
	private JTextField txtdescripcion;
	private JLabel lblprecio;
	private JTextField txtprecio;
	private JButton btnsalir;
	private JButton btncancelar;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JLabel jLabel2;
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				VistaProducto inst = new VistaProducto();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public VistaProducto() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			{
				this.setSize(440, 351);
			}
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Productos");
			getContentPane().setLayout(null);
			
				
				
			{
				btnagregar = new JButton();
				getContentPane().add(btnagregar);
				btnagregar.setText("Agredar Ingrediente");
				btnagregar.setBounds(150, 283, 220, 22);
				btnagregar.setActionCommand("Agregar");
			}
			{
				btnquitar = new JButton();
				getContentPane().add(btnquitar);
				btnquitar.setText("Quitar Ingrediente");
				btnquitar.setBounds(150, 322, 220, 22);
				btnquitar.setActionCommand("Quitar");
			}
			{
				Vector<String> v = new Vector<String>();
				String patron = "0.0";
				DecimalFormat nf = new DecimalFormat(patron); 
				nf.applyPattern(patron);
				
				double i =0.1;
				while(i<=100){
					v.add(nf.format(i));
					i+=0.1;
					
				}
				SpinnerListModel spnProcionModel = 
					new SpinnerListModel(
							new String[] { "Sun", "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" });
				spnProcion = new JSpinner();
				getContentPane().add(spnProcion);
				spnProcionModel.setList(v);
				spnProcion.setBounds(168, 242, 173, 22);
				spnProcion.setModel(spnProcionModel);
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Porcion");
				jLabel1.setBounds(199, 227, 103, 15);
			}
			{
				modeloIngrediente = 
					new DefaultTableModel(null, new String[] { "Ingrediente"});
				tblIngrediente = new JTable();
				getContentPane().add(tblIngrediente);
				tblIngrediente.setModel(modeloIngrediente);
				tblIngrediente.setBounds(6, 233, 138, 147);
			}
			{
				modeloAgregados = 
					new DefaultTableModel(null, new String[] { "Ingrediente", "Porcion" });
				tblAgregados = new JTable();
				getContentPane().add(tblAgregados);
				tblAgregados.setModel(modeloAgregados);
				tblAgregados.setBounds(379, 235, 229, 150);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Ingredientes Registrados ");
				jLabel2.setBounds(10, 212, 189, 15);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Ingrediente ");
				jLabel3.setBounds(379, 212, 110, 15);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Porcion");
				jLabel4.setBounds(533, 212, 81, 15);
			}
			{
				btncancelar = new JButton();
				getContentPane().add(btncancelar);
				btncancelar.setText("Cancelar");
				btncancelar.setBounds(337, 403, 101, 22);
				btncancelar.setActionCommand("Cancelar");
			}
			{
				btnsalir = new JButton();
				getContentPane().add(btnsalir);
				btnsalir.setText("Salir");
				btnsalir.setBounds(235, 403, 91, 22);
				btnsalir.setActionCommand("Salir");
			}
			{
				txtprecio = new JTextFieldValidator(JTextFieldValidator.SOLO_NUMEROS);
				getContentPane().add(txtprecio);
				txtprecio.setBounds(293, 134, 159, 23);
			}
			{
				lblprecio = new JLabel();
				getContentPane().add(lblprecio);
				lblprecio.setText("Precio");
				lblprecio.setBounds(181, 137, 94, 15);
			}
			{
				txtdescripcion = new JTextFieldValidator(JTextFieldValidator.SOLO_LETRAS);
				getContentPane().add(txtdescripcion);
				txtdescripcion.setBounds(295, 48, 157, 24);
			}
			{
				lbldescripcion = new JLabel();
				getContentPane().add(lbldescripcion);
				lbldescripcion.setText("Descripcion");
				lbldescripcion.setBounds(181, 52, 116, 15);
			}
			{
				txtcodigo = new JTextFieldValidator(JTextFieldValidator.SOLO_NUMEROS);
				getContentPane().add(txtcodigo);
				txtcodigo.setBounds(293, 9, 161, 21);
			}
			{
				lblcodigo = new JLabel();
				getContentPane().add(lblcodigo);
				lblcodigo.setText("Codigo");
				lblcodigo.setBounds(181, 12, 94, 15);
			}
			{
				ComboBoxModel cmbcategoriaModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				cmbcategoria = new JComboBox();
				getContentPane().add(cmbcategoria);
				cmbcategoria.setModel(cmbcategoriaModel);
				cmbcategoria.setBounds(294, 93, 158, 22);
			}
			{
				lblcategoria = new JLabel();
				getContentPane().add(lblcategoria);
				lblcategoria.setText("Categoria");
				lblcategoria.setBounds(181, 97, 101, 15);
			}
			{
				btnregistrar = new JButton();
				getContentPane().add(btnregistrar);
				btnregistrar.setText("Registrar");
				btnregistrar.setBounds(106, 403, 115, 22);
				btnregistrar.setActionCommand("Registrar");
			}
			pack();
			this.setSize(636, 466);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	
	
	public void addlistener(ActionListener actionlistener){
    	btnagregar.addActionListener(actionlistener);
    	btncancelar.addActionListener(actionlistener);
    	btnquitar.addActionListener(actionlistener);
    	btnsalir.addActionListener(actionlistener);
    	btnregistrar.addActionListener(actionlistener);
    }

	
	public void limpiarCancelar(){
		txtcodigo.setText("");
		txtdescripcion.setText("");
		txtprecio.setText("");
		modeloAgregados.setRowCount(0);
		cmbcategoria.setSelectedIndex(0);
		}
	
	public float getPorcion(){
		String s = ""+spnProcion.getValue();		
		return Float.parseFloat(s.substring(0,s.indexOf(","))) + Float.parseFloat(s.substring(s.indexOf(",")+1,s.length()));
		
	}

	public JTable getTblIngrediente() {
		return tblIngrediente;
	}


	public JTable getTblAgregados() {
		return tblAgregados;
	}

	

	public DefaultTableModel getModeloIngrediente() {
		return modeloIngrediente;
	}

	

	public DefaultTableModel getModeloAgregados() {
		return modeloAgregados;
	}



	public JComboBox getCmbcategoria() {
		return cmbcategoria;
	}

	public void setCmbcategoria(JComboBox cmbcategoria) {
		this.cmbcategoria = cmbcategoria;
	}

	public String getcategoria() {
		return (String)cmbcategoria.getItemAt(cmbcategoria.getSelectedIndex());
	}


	public String getcodigo() {
		return txtcodigo.getText();
	}

	public String getdescripcion() {
		return txtdescripcion.getText();
	}

	

	public String getprecio() {
		return txtprecio.getText();
	}
	
	public void quitar() {
		int i = tblAgregados.getSelectedRow();
		if(i!=-1){
			modeloAgregados.removeRow(i);
		}
		else
			mostrarMensaje("Selecione el ingrediente que desea quitar");
	}

	public void agregar() {
		int j = tblIngrediente.getSelectedRow();
		if(j!=-1){
			int i =0;
			boolean sw = true;
			while(i< tblAgregados.getRowCount()) {
				if(tblAgregados.getValueAt(i,0).equals(tblIngrediente.getValueAt(i,0))){
					tblAgregados.setValueAt(getPorcion(), i, 1);
					return;
				}			
				else
					i++;
			}
			if(i!=tblAgregados.getRowCount() || tblAgregados.getRowCount()==0){
				Vector fila = new Vector();
				fila.add(tblIngrediente.getValueAt(i,0));
				fila.add(getPorcion());
				modeloAgregados.addRow(fila);
			}
				
		}
		else
			mostrarMensaje("Selecione el ingrediente que desea agregar");
		
	}
	
	public void mostrarMensaje(String mensaje) {
		JOptionPane.showMessageDialog(this, mensaje);
	}

	public void setTxtcodigo(JTextField txtcodigo) {
		this.txtcodigo = txtcodigo;
	}

	public void setTxtdescripcion(JTextField txtdescripcion) {
		this.txtdescripcion = txtdescripcion;
	}

	public void setTxtprecio(JTextField txtprecio) {
		this.txtprecio = txtprecio;
	}

	public JTextField getTxtcodigo() {
		return txtcodigo;
	}

	public JTextField getTxtdescripcion() {
		return txtdescripcion;
	}

	public JTextField getTxtprecio() {
		return txtprecio;
	}

	
	
}
